
Namespace language
    Namespace japanese
        Namespace trieSolution
            Public Module trieSolution
                Public Const sbcKataCharCount As Int32 = 94

                Public Function sbcKataToIndex(ByRef c As Char) As UInt32
                    Return Convert.ToUInt32(c) - Convert.ToUInt32("0x30a1", 16)
                End Function

                Public Const hiraCharCount As Int32 = 95

                Public Function hiraToIndex(ByRef c As Char) As UInt32
                    Dim temp As UInt32
                    Dim rtn As UInt32
                    rtn = Convert.ToUInt32(c)
                    temp = Convert.ToUInt32("0x3041", 16)
                    If rtn < temp Then
                        rtn = 0
                    Else
                        rtn = rtn - temp
                    End If
                    Return rtn
                End Function

                Public ReadOnly sbcCharCount As Int32 = _
                                hiraCharCount + sbcKataCharCount + CJKShare.hanziUpbound - CJKShare.hanziLowerbound + 1

                Public Function sbcCharToIndex(ByRef c As Char) As UInt32
                    Dim rtn As UInt32
                    If ishira(c) Then
                        rtn = hiraToIndex(c)
                    ElseIf issbckata(c) Then
                        rtn = sbcKataToIndex(c) + hiraCharCount
                    ElseIf CJKShare.isCJKHanzi(c) Then
                        rtn = CJKShare.CJKHanziToIndex(c) + hiraCharCount + sbcKataCharCount
                    End If

                    Return rtn
                End Function

            End Module
        End Namespace

        Public Module japanese

            Friend _sbckatalong As Char = hextochar("0x30fc")
            Friend _dbckatalong As Char = hextochar("0xff70")
            Friend _sbckatadakuon As Char = hextochar("0x30c3")
            Friend _dbckatadakuon As Char = hextochar("0xff6f")

            Public Function sbckatalong() As Char
                Return _sbckatalong
            End Function

            Public Function dbckatalong() As Char
                Return _dbckatalong
            End Function

            Public Function sbckatadakuon() As Char
                Return _sbckatadakuon
            End Function

            Public Function dbckatadakuon() As Char
                Return _dbckatadakuon
            End Function

            Public Function iskatalong(ByVal c As Char) As Boolean
                Return strsame(c, _sbckatalong) OrElse strsame(c, _dbckatalong)
            End Function

            Public Function iskatadakuon(ByVal c As Char) As Boolean
                Return strsame(c, _sbckatadakuon) OrElse strsame(c, _dbckatadakuon)
            End Function

            Public Function iskataassimilatesound(ByVal c As Char) As Boolean
                Return iskatadakuon(c)
            End Function

            Public Function issbckatahead(ByVal c As Char) As Boolean
                If issbckata(c) Then
                    Return c <> hextochar("0x30a1") AndAlso _
                            c <> hextochar("0x30a3") AndAlso _
                            c <> hextochar("0x30a5") AndAlso _
                            c <> hextochar("0x30a7") AndAlso _
                            c <> hextochar("0x30a9") AndAlso _
                            c <> hextochar("0x30c3") AndAlso _
                            c <> hextochar("0x30e3") AndAlso _
                            c <> hextochar("0x30e5") AndAlso _
                            c <> hextochar("0x30e7") AndAlso _
                            c <> hextochar("0x30ee") AndAlso _
                            c <> hextochar("0x30f5") AndAlso _
                            c <> hextochar("0x30f6") AndAlso _
                            c <> hextochar("0x30fb") AndAlso _
                            c <> hextochar("0x30fc")
                Else
                    Return False
                End If
            End Function

            Public Function issbckatatail(ByVal c As Char) As Boolean
                If issbckata(c) Then
                    Return c <> hextochar("0x30fb") AndAlso _
                            c <> hextochar("0x30c3")
                Else
                    Return False
                End If
            End Function

            Public Function isdbckatahead(ByVal c As Char) As Boolean
                Dim int_c As Int32 = Convert.ToInt32(c)
                If isdbckata(c) Then
                    Return (int_c >= Convert.ToInt32("0xff71", 16) AndAlso _
                            int_c <= Convert.ToInt32("0xff9d", 16)) OrElse _
                            int_c <= Convert.ToInt32("0xff64", 16)
                Else
                    Return False
                End If
            End Function

            Public Function isdbckatatail(ByVal c As Char) As Boolean
                If isdbckata(c) Then
                    Return c >= hextochar("0xff66") AndAlso _
                            c <= hextochar("0xff9d") AndAlso _
                            c <> hextochar("0xff6f")
                Else
                    Return False
                End If
            End Function

            Public Function iskatahead(ByVal c As Char) As Boolean
                Return issbckatahead(c) OrElse isdbckatahead(c)
            End Function

            Public Function iskatatail(ByVal c As Char) As Boolean
                Return issbckatatail(c) OrElse isdbckatatail(c)
            End Function

            Public Function isdbckata(ByVal c As Char) As Boolean
                Return c <= hextochar("0xff9f") AndAlso _
                        c >= hextochar("0xff66")
            End Function

            Public Function issbckata(ByVal c As Char) As Boolean
                Return (c <= hextochar("0x30fe") AndAlso _
                        c >= hextochar("0x30a1"))
            End Function

            Public Function iskata(ByVal c As Char) As Boolean
                Return isdbckata(c) OrElse issbckata(c)
            End Function

            Public Function ishira(ByVal c As Char) As Boolean
                Return (c >= hextochar("0x3041") AndAlso _
                        c <= hextochar("0x3093")) OrElse _
                        c = hextochar("0x3005")
            End Function

            Public Function iskataorspace(ByVal c As Char) As Boolean
                Return iskata(c) OrElse isspace(c)
            End Function

            Public Function issbckataword(ByRef str As String) As Boolean
                Dim rtn As Boolean = strform(str, AddressOf issbckata)
                If rtn Then
                    If strlen(str) > 0 Then
                        rtn = issbckatahead(str(0)) AndAlso issbckatatail(str(strlen(str) - 1))
                    End If
                End If

                Return rtn
            End Function

            Public Function isdbckataword(ByRef str As String) As Boolean
                Dim rtn As Boolean = strform(str, AddressOf isdbckata)
                If rtn Then
                    If strlen(str) > 0 Then
                        rtn = isdbckatahead(str(0)) AndAlso isdbckatatail(str(strlen(str) - 1))
                    End If
                End If

                Return rtn
            End Function

            Public Function iskataword(ByRef str As String) As Boolean
                Return isdbckataword(str) OrElse issbckataword(str)
            End Function

            Public Function ismixkataword(ByRef str As String) As Boolean
                Dim rtn As Boolean = strform(str, AddressOf iskata)
                If rtn Then
                    If strlen(str) > 0 Then
                        rtn = iskatahead(str(0)) AndAlso iskatatail(str(strlen(str) - 1))
                    End If
                End If

                Return rtn
            End Function

            Public Function ishiraorspace(ByVal c As Char) As Boolean
                Return ishira(c) OrElse isspace(c)
            End Function

            Public Function ishiraword(ByRef str As String) As Boolean
                Return strform(str, AddressOf ishiraorspace)
            End Function

            Public Function iskana(ByVal c As Char) As Boolean
                Return iskata(c) OrElse ishira(c)
            End Function

            Public Function possibleChar(ByVal c As Char) As Boolean
                Return iskana(c) OrElse CJKShare.isCJKHanzi(c)
            End Function

            Public Function sbcPossibleChar(ByVal c As Char) As Boolean
                Return ishira(c) OrElse issbckata(c) OrElse CJKShare.isCJKHanzi(c)
            End Function

            Public Function issbcword(ByRef s As String) As Boolean
                Return strform(s, AddressOf japanese.sbcPossibleChar)
            End Function
        End Module
    End Namespace
End Namespace
